/******************************************************************************

  File Name     : alg_api.cpp
  Version       : Initial Draft
  Author        : tongzhilin
  Created       : 2020/8/7
  Last Modified :
  Description   : alg_api
  Function List :
  History       :
  1.Date        : 2020/8/7
    Author      : tongzhilin
    Modification: Created file

******************************************************************************/
#include "mp_common.h"
#include "alg_struct.h"
#ifdef INFERENCE_NNIE
#include "nnie_algbasic.h"
#endif
#ifdef INFERENCE_MXNET
#include "mx_algbasic.h"
#endif
#ifdef INFERENCE_CAFFE
#include "caffe_algbasic.h"
#endif
#include "alg_face_detect_v1.h"
#include "alg_face_lmk_v1.h"
#include "alg_face_feature_v1.h"

INT32       Alg_Module_Test(ALG_INIT_PARAMS* _pstInitParams)
{
    CHECK_NON_NULL_POINTER(_pstInitParams, -1);
#ifdef INFERENCE_NNIE
    MP_LOG_INFO("Alg module nnie test start!\n");
    NnieAlgBasic* pstAlgTest = new NnieAlgBasic(_pstInitParams->unModel.stNnie.m_cWkFile);
    delete pstAlgTest;
    MP_LOG_INFO("Alg module nnie test end!\n");
#endif
    return 0;
}

ALG_HANDLE  Alg_Face_Detect_Init(ALG_INIT_PARAMS* _pstInitParams)
{
    CHECK_NON_NULL_POINTER(_pstInitParams, NULL);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceDet = new FaceDetV1(_pstInitParams->unModel.stNnie.m_cWkFile);
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceDet = new FaceDetV1(_pstInitParams->unModel.stMxnet.m_cJsonFile, 
        _pstInitParams->unModel.stMxnet.m_cParamsFile);
#endif
    return (ALG_HANDLE)pstFaceDet;
}
INT32       Alg_Face_Detect_DeInit(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceDet = (NnieAlgBasic*)_pHandle;
    delete pstFaceDet;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceDet = (MxAlgBasic*)_pHandle;
    delete pstFaceDet;
#endif
    return 0;
}

ALG_HANDLE  Alg_Face_LMK_Init(ALG_INIT_PARAMS* _pstInitParams)
{
    CHECK_NON_NULL_POINTER(_pstInitParams, NULL);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceLmk = new FacelmkV1(_pstInitParams->unModel.stNnie.m_cWkFile);
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceLmk = new FacelmkV1(_pstInitParams->unModel.stMxnet.m_cJsonFile, 
        _pstInitParams->unModel.stMxnet.m_cParamsFile);
#endif
    return (ALG_HANDLE)pstFaceLmk;
}
INT32       Alg_Face_LMK_DeInit(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceLmk = (NnieAlgBasic*)_pHandle;
    delete pstFaceLmk;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceLmk = (MxAlgBasic*)_pHandle;
    delete pstFaceLmk;
#endif
    return 0;
}
ALG_HANDLE  Alg_Face_Feature_Init(ALG_INIT_PARAMS* _pstInitParams)
{
    CHECK_NON_NULL_POINTER(_pstInitParams, NULL);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceFeature = new FaceFeatureV1(_pstInitParams->unModel.stNnie.m_cWkFile);
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceFeature = new FaceFeatureV1(_pstInitParams->unModel.stMxnet.m_cJsonFile, 
        _pstInitParams->unModel.stMxnet.m_cParamsFile);
#endif
    return (ALG_HANDLE)pstFaceFeature;
}
INT32       Alg_Face_Feature_DeInit(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceFeature = (NnieAlgBasic*)_pHandle;
    delete pstFaceFeature;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceFeature = (MxAlgBasic*)_pHandle;
    delete pstFaceFeature;
#endif
    return 0;
}
INT32       Alg_Face_Forward_Image(ALG_HANDLE _pHandle, ALG_IMAGE_S* _pstImage, INT32 _s32MaxRet, void *_pstResult)
{
    INT32 s32Ret = 0;
    CHECK_NON_NULL_POINTER(_pHandle, -1);
    CHECK_NON_NULL_POINTER(_pstImage, -1);
    CHECK_NON_NULL_POINTER(_pstResult, -1);

#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceAlg = (NnieAlgBasic*)_pHandle;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceAlg = (MxAlgBasic*)_pHandle;
#endif
    s32Ret = pstFaceAlg->alg_run_image(_pstImage, _s32MaxRet, _pstResult);

    return s32Ret;
}
INT32       Alg_Face_Forward_Image_Rects(ALG_HANDLE _pHandle, ALG_IMAGE_S* _pstImage, 
    ALG_RECT_S *_pstRect, INT32 _s32RectNum, void *_pstResult)
{
    return 0;
}

INT32       Alg_Face_Forward_Image_Lmk5pts(ALG_HANDLE _pHandle, ALG_IMAGE_S* _pstImage, 
    ALG_FACELMK_RESULT_S *_pstLmk, INT32 _s32FaceNum, void *_pstResult)
{
    return 0;
}

INT32       Alg_Face_Get_Input_C(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceAlg = (NnieAlgBasic*)_pHandle;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceAlg = (MxAlgBasic*)_pHandle;
#endif
    return pstFaceAlg->alg_get_input_c();
}
INT32       Alg_Face_Get_Input_H(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceAlg = (NnieAlgBasic*)_pHandle;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceAlg = (MxAlgBasic*)_pHandle;
#endif
    return pstFaceAlg->alg_get_input_h();
}
INT32       Alg_Face_Get_Input_W(ALG_HANDLE _pHandle)
{
    CHECK_NON_NULL_POINTER(_pHandle, -1);
#ifdef INFERENCE_NNIE
    NnieAlgBasic* pstFaceAlg = (NnieAlgBasic*)_pHandle;
#endif
#ifdef INFERENCE_MXNET
    MxAlgBasic* pstFaceAlg = (MxAlgBasic*)_pHandle;
#endif
    return pstFaceAlg->alg_get_input_w();
}

INT32       Alg_Face_Feature_Cmp_Consin(ALG_FACEFEATURE_RESULT_S* _pstFeat1, ALG_FACEFEATURE_RESULT_S* _pstFeat2)
{
    CHECK_NON_NULL_POINTER(_pstFeat1, -1);
    CHECK_NON_NULL_POINTER(_pstFeat2, -1);
#ifdef INFERENCE_NNIE
    float ret = NnieAlgBasic::alg_feature_compare_consin(_pstFeat1, _pstFeat2);
#endif
#ifdef INFERENCE_MXNET
    float ret = MxAlgBasic::alg_feature_compare_consin(_pstFeat1, _pstFeat2);
#endif
    return ret;
}
INT32       Alg_Face_Feature_Cmp_Euclidean(ALG_FACEFEATURE_RESULT_S* _pstFeat1, ALG_FACEFEATURE_RESULT_S* _pstFeat2)
{
    CHECK_NON_NULL_POINTER(_pstFeat1, -1);
    CHECK_NON_NULL_POINTER(_pstFeat2, -1);
#ifdef INFERENCE_NNIE
    float ret = NnieAlgBasic::alg_feature_compare_euclidean(_pstFeat1, _pstFeat2);
#endif
#ifdef INFERENCE_MXNET
    float ret = MxAlgBasic::alg_feature_compare_euclidean(_pstFeat1, _pstFeat2);
#endif
    return ret;
}

